package com.hsq.ndmall.promotion.service.impl;

import com.hsq.ndmall.common.utils.R;
import com.hsq.ndmall.promotion.feign.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

import com.hsq.ndmall.promotion.dao.GrouponRulesDao;
import com.hsq.ndmall.promotion.entity.GrouponRulesEntity;
import com.hsq.ndmall.promotion.service.GrouponRulesService;


@Service("grouponRulesService")
public class GrouponRulesServiceImpl extends ServiceImpl<GrouponRulesDao, GrouponRulesEntity> implements GrouponRulesService {

    @Autowired
    GrouponRulesDao rulesDao;

    // 远程调用
    @Autowired
    ProductFeign productFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GrouponRulesEntity> page = this.page(
                new Query<GrouponRulesEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    private QueryWrapper getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<GrouponRulesEntity> queryWrapper = new QueryWrapper<>();
        //判断Map中是否存在goods_id的健
        if (params.containsKey("goods_id")) {
            queryWrapper.lambda().eq(GrouponRulesEntity::getGoodsId,params.get("goods_id"));
        }
        if(params.containsKey("status")){
            //queryWrapper.lambda().eq(GrouponRulesEntity::get)
        }
        return queryWrapper;
    }

    @Override
    public List<Map> selectByGrouponsList(Map<String, Object> params) {

        // 查询所有团购规则
        List<GrouponRulesEntity> grouponRulesEntities = rulesDao.selectList(null);
        List<Map> mapList = new ArrayList<>();

        for (GrouponRulesEntity el : grouponRulesEntities) {
            Map<String, Object> map = new HashMap<>();
            R info = productFeign.info(el.getGoodsId());
            // 查询出商品信息
            map.put("name", el.getGoodsName());
            map.put("brief", ((Map) info.get("goods")).get("name"));
            map.put("counterPrice", ((Map) info.get("goods")).get("counterPrice"));
            map.put("retailPrice", ((Map) info.get("goods")).get("retailPrice"));
            map.put("id", el.getGoodsId());
            map.put("picUrl", el.getPicUrl());
            map.put("grouponDiscount", el.getDiscount());
            map.put("grouponMember", el.getDiscountMember());
            map.put("grouponPrice", sub(map.get("retailPrice"), el.getDiscount()));
            mapList.add(map);
        }

        return mapList;
    }

    private BigDecimal sub(Object retailPrice, BigDecimal discount) {
        BigDecimal bigDecimal = new BigDecimal((Double) retailPrice);
        return bigDecimal.subtract(discount);
    }


}
